var config = require('../../../utils/config');
var API_URL = config.API_URL;
var login = require('../../../utils/login');
var util = require('../../../utils/util');
var app = getApp();
let recommendId;
let Pay = util.wxPromise(my.tradePay);
Page({
    data: {
    },
    onLoad: function (options) {
        console.log(options)
        // 页面初始化 options为页面跳转所带来的参数
        var that = this;
        recommendId = options.recommendId;
        util.PRL(API_URL.address_list, {})
            .then(res => {
                console.log(res);
                let address = res.data.data.data;
                if (address.length != 0) {
                    app.globalData.address = address[0];
                } else {
                    app.globalData.address = null;
                }
                that.setData({
                    address: app.globalData.address,
                    good: app.globalData.good
                })
            }).catch(res => {
                console.log(res)
            })
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        var that = this;
        if (that.data.address && that.data.address.addressId != app.globalData.address.addressId) {
            that.setData({
                address: app.globalData.address
            })
        }
    },
    // 设置收获地址
    choose_addr: function (e) {
        my.navigateTo({
            url: '/pages/address/address',
        })
    },
    pay: function (e) {
        var that = this;
        if (typeof (that.data.address.addressId) == "undefined") {
            util.showNotice('请选择收货地址');
            return
        }
        let params = {
            type: 0,
            goods_id: that.data.good.id
        }
        util.PRL(API_URL.cart_settle, params)
            .then(res => {
                console.log(res)
                let cartId = res.data.data.cartId;
                return util.PRL(API_URL.order, {
                    address_id: that.data.address.addressId,
                    cart_id: cartId,
                    leave_comments: that.data.liuyan
                })
            }).then(res => {
                console.log(res);
                // let openId = my.getStorageSync({key:'openId'}).data;
                let userInfo = my.getStorageSync({key:'user'}).data;
                return util.POST(API_URL.ali_pay, {
                    order_number:res.data.data.orderNumber
                    // tag: 'tianguangxu',
                    // userId: userInfo.userId,
                    // outTradeNumber: res.data.data.batchNumber,
                    // body: res.data.data.goods[0].name,
                    // // totalFee: Number(res.data.data.amount) * 100,
                    // totalFee: 1,
                    // // openid: openId
                    // // notifyUrl: 'https://mina.jade-box.com/cloudMasses/pay/yudu/notify '
                })
            }).then(res => {
                console.log(res)
                // let timeStamp = res.data.sign.timeStamp.toString();
                return Pay({
                    orderStr:res.data.data.url
                    // timeStamp: timeStamp,
                    // orderStr: res.data.sign.nonceStr
                    // package: res.data.sign.package,
                    // signType: 'MD5',
                    // paySign: res.data.sign.sign,
                })
            }).then(res => {
                console.log(res)
                app.globalData.goToOrder = true;
                my.switchTab({
                    url: '/pages/user/user'
                })
            }).catch(res => {
                console.log(res)
                if (res.errMsg == 'requestPayment:fail cancel') {
                    app.globalData.goToOrder = true;
                    my.switchTab({
                        url: '/pages/user/index'
                    })
                }
            })
    }
})